package com.hero.updown;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.engine.UriToFileTransformEngine;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnKeyValueResultCallbackListener;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.luck.picture.lib.utils.SandboxTransformUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.io.File;
import java.text.NumberFormat;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_PERMISSION_STORAGE = 0x01;

    private NumberFormat numberFormat;
    private ProgressBar firstProgressBar;
    private TextView tv_updown_path_title;
    private TextView tv_updown_path;
    private TextView tv_save_path;
    private LinearLayout ll_content;
    private LinearLayout ll_bt;
    private Button bt_close;

    private  UpUtils mUpUtils;
    private  DownUtils mDownUtils;

    private String downSavePath;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        downSavePath = Environment.getExternalStorageDirectory() +"/UpDownDemo";
        mUpUtils = new UpUtils(this);
        mDownUtils = new DownUtils(this);
        findViewById(R.id.bt_up).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectImage();
            }
        });
//
        findViewById(R.id.bt_down).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDownUtils.down("http://ucan.25pp.com/Wandoujia_web_seo_baidu_homepage.apk",downSavePath);
            }
        });

        checkSDCardPermission();

    }


    private void selectImage() {
        PictureSelector.create(this)
                .openGallery(SelectMimeType.ofAll())
                .setMaxSelectNum(1)
                .setMinSelectNum(1)
                .setSandboxFileEngine(new UriToFileTransformEngine() {
                    @Override
                    public void onUriToFileAsyncTransform(Context context, String srcPath, String mineType, OnKeyValueResultCallbackListener call) {
                        if (call != null) {
                            String sandboxPath = SandboxTransformUtils.copyPathToSandbox(context, srcPath, mineType);
                            call.onCallback(srcPath,sandboxPath);
                        }
                    }
                })
                .setImageEngine(GlideEngine.createGlideEngine())
                .forResult(new OnResultCallbackListener<LocalMedia>() {
                    @Override
                    public void onResult(ArrayList<LocalMedia> result) {
                        String path = "";
                        if (result.size() > 0){
                             path = result.get(0).getRealPath();
                        }
                        if (!TextUtils.isEmpty(path)){
                            mUpUtils.upload("http://hmain.xingksj.cn/upload.ashx", path, new StatusCallback() {
                                @Override
                                public void ok() {
                                    Log.e("111","操作完成  进行同步操作");
                                }
                            });
                        }

                    }

                    @Override
                    public void onCancel() {

                    }
                });
    }

    /** 检查SD卡权限 */
    protected void checkSDCardPermission() {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSION_STORAGE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION_STORAGE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //获取权限
            } else {
                Toast.makeText(MainActivity.this,"权限被禁止，无法下载文件！",Toast.LENGTH_LONG).show();
            }
        }
    }



}